package com.music.vedio.dao.impl;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.music.domain.PageInfo;
import com.music.domain.Video;
import com.music.vedio.dao.VedioDao;

@Repository
public class VedioDaoImpl implements VedioDao {
	@Autowired
	SessionFactory sessionFactory;
	
	@Override
	public List<Video> findVedioAll(Map param, PageInfo page) {
		String sql="SELECT COUNT(*) FROM T_VIDEO";
		String hql="from Video";
		SQLQuery sqlQuery = sessionFactory.getCurrentSession().createSQLQuery(sql);
		Query hqlQuery = sessionFactory.getCurrentSession().createQuery(hql);
		BigDecimal number = (BigDecimal) sqlQuery.uniqueResult();
		page.setNumber(String.valueOf(number));
		page.setTotal(String.valueOf((int)Math.ceil(Double.parseDouble(page.getNumber())/Double.parseDouble(page.getCount()))));
		hqlQuery.setFirstResult((Integer.parseInt(page.getCurr_total())-1)*(Integer.parseInt(page.getCount())));
		hqlQuery.setMaxResults(Integer.parseInt(page.getCount()));
		List<Video> data = hqlQuery.list();
		return data;
	}
	

}
